package com.ly.heart.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.utils.DateUtils;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.FileEntity;
import com.ly.heart.domain.SurveyRefereeAgreementEntity;
import com.ly.heart.domain.SurveyRefereeCompanyEntity;
import com.ly.heart.domain.SurveyRefereeEntity;
import com.ly.heart.domain.dto.SurveyRefereeDto;
import com.ly.heart.mapper.SurveyRefereeMapper;
import com.ly.heart.service.IFileInfoService;
import com.ly.heart.service.ISurveyRefereeAgreementService;
import com.ly.heart.service.ISurveyRefereeCompanyService;
import com.ly.heart.service.ISurveyRefereeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 勘测合作方/路条人信息Service业务层处理
 * @author wutao
 * @date 2024-05-22
 */
@Service
public class SurveyRefereeServiceImpl extends ServiceImpl<SurveyRefereeMapper, SurveyRefereeEntity> implements ISurveyRefereeService {

    @Resource
    private SurveyRefereeMapper erpSurveyRefereeInfoMapper;
    @Resource
    private ISurveyRefereeCompanyService iSurveyRefereeCompanyService;
    @Resource
    private ISurveyRefereeAgreementService iSurveyRefereeAgreementService;
    @Resource
    private IFileInfoService iFileInfoService;


    /**
     * 查询勘测合作方/路条人信息
     * @param id 勘测合作方/路条人信息ID
     * @return 勘测合作方/路条人信息
     */
    @Override
    public SurveyRefereeEntity selectErpSurveyRefereeInfoById (String id) {
        return erpSurveyRefereeInfoMapper.selectErpSurveyRefereeInfoById ( id );
    }

    /**
     * 查询勘测合作方/路条人信息列表
     * @param erpSurveyRefereeInfo 勘测合作方/路条人信息
     * @return 勘测合作方/路条人信息
     */
    @Override
    public List<SurveyRefereeEntity> selectErpSurveyRefereeInfoList (SurveyRefereeEntity erpSurveyRefereeInfo) {
        return erpSurveyRefereeInfoMapper.selectErpSurveyRefereeInfoList ( erpSurveyRefereeInfo );
    }

    /**
     * 新增勘测合作方/路条人信息
     * @param surveyRefereeDto 勘测合作方/路条人信息
     * @return 结果
     */
    @Override
    @Transactional (propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int insertErpSurveyRefereeInfo (SurveyRefereeDto surveyRefereeDto) {
        //1.处理路条人基础嘻嘻
        SurveyRefereeEntity surveyRefereeEntity = surveyRefereeDto.getSurveyRefereeEntity ();
        String surveyRefereeId = SnowflakeIdWorker.getInstance ().nextStringId ();
        surveyRefereeEntity.setId ( surveyRefereeId );
        surveyRefereeEntity.setCreateTime ( DateUtils.getNowDate () );
        boolean saveReferee = save ( surveyRefereeEntity );
        if ( ! saveReferee ) {
            throw new RuntimeException ( "新增路条人基本信息失败" );
        }
        //2.处理公司主体
        SurveyRefereeCompanyEntity surveyRefereeCompanyEntity = surveyRefereeDto.getSurveyRefereeCompanyEntity ();
        if ( surveyRefereeCompanyEntity != null ) {
            String companyId = SnowflakeIdWorker.getInstance ().nextStringId ();
            surveyRefereeCompanyEntity.setId ( companyId );
            surveyRefereeCompanyEntity.setSurveyRefereeId ( surveyRefereeId );
            //surveyRefereeCompanyEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
            surveyRefereeCompanyEntity.setCreateTime ( DateUtils.getNowDate () );
            boolean save = iSurveyRefereeCompanyService.save ( surveyRefereeCompanyEntity );
            if ( ! save ) {
                throw new RuntimeException ( "新增路条人主体公司失败" );
            }
            //处理公司主体的附件
            updateFile ( surveyRefereeCompanyEntity.getFileEntities (), companyId );
        }
        //3.处理路条人居间协议
        SurveyRefereeAgreementEntity surveyRefereeAgreementEntity = surveyRefereeDto.getSurveyRefereeAgreementEntity ();
        if ( surveyRefereeAgreementEntity != null ) {
            String agreementId = SnowflakeIdWorker.getInstance ().nextStringId ();
            surveyRefereeAgreementEntity.setId ( agreementId );
            surveyRefereeAgreementEntity.setRefereeId ( surveyRefereeId );
            //surveyRefereeAgreementEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
            surveyRefereeAgreementEntity.setCreateTime ( DateUtils.getNowDate () );
            boolean saveAgreement = iSurveyRefereeAgreementService.save ( surveyRefereeAgreementEntity );
            if ( ! saveAgreement ) {
                throw new RuntimeException ( "新增路条人居间协议失败" );
            }
            //处理居间协议的附件
            updateFile ( surveyRefereeCompanyEntity.getFileEntities (), agreementId );
        }
        //4.处理附件
        updateFile ( surveyRefereeEntity.getFileEntities (), surveyRefereeId );
        return 1;
    }

    /**
     * 修改勘测合作方/路条人信息
     * @param surveyRefereeDto 勘测合作方/路条人信息
     * @return 结果
     */
    @Override
    public int updateErpSurveyRefereeInfo (SurveyRefereeDto surveyRefereeDto) {
        //1.处理路条人基础嘻嘻
        SurveyRefereeEntity surveyRefereeEntity = surveyRefereeDto.getSurveyRefereeEntity ();
        surveyRefereeEntity.setChangeTime ( DateUtils.getNowDate () );
        int i = baseMapper.updateErpSurveyRefereeInfo ( surveyRefereeEntity );
        if ( i == 0 ) {
            throw new RuntimeException ( "修改路条人基本信息失败" );
        }
        //2.处理公司主体
        SurveyRefereeCompanyEntity surveyRefereeCompanyEntity = surveyRefereeDto.getSurveyRefereeCompanyEntity ();
        if ( surveyRefereeCompanyEntity != null ) {
            surveyRefereeCompanyEntity.setSurveyRefereeId ( surveyRefereeEntity.getId () );
            int updateCompany = iSurveyRefereeCompanyService.updateRefereeCompanyByRefereeId ( surveyRefereeCompanyEntity );
            if ( updateCompany == 0 ) {
                throw new RuntimeException ( "修改路条人主体公司失败" );
            }
        }
        //3.处理路条人居间协议
        SurveyRefereeAgreementEntity surveyRefereeAgreementEntity = surveyRefereeDto.getSurveyRefereeAgreementEntity ();
        if ( surveyRefereeAgreementEntity != null ) {
            surveyRefereeAgreementEntity.setRefereeId ( surveyRefereeEntity.getId () );
            int updateAgreement = iSurveyRefereeAgreementService.updateRefereeAgreementByRefereeId ( surveyRefereeAgreementEntity );
            if ( updateAgreement == 0 ) {
                throw new RuntimeException ( "新增路条人居间协议失败" );
            }
            //处理居间协议的附件
            //updateFile ( surveyRefereeCompanyEntity.getFileEntities (), agreementId );
        }
        //4.处理附件
        updateFile ( surveyRefereeEntity.getFileEntities (), surveyRefereeEntity.getId () );
        return 1;
    }

    /**
     * 批量删除勘测合作方/路条人信息
     * @param ids 需要删除的勘测合作方/路条人信息ID
     * @return 结果
     */
    @Override
    public int deleteErpSurveyRefereeInfoByIds (String[] ids) {
        return erpSurveyRefereeInfoMapper.deleteErpSurveyRefereeInfoByIds ( ids );
    }

    /**
     * 删除勘测合作方/路条人信息信息
     * @param id 勘测合作方/路条人信息ID
     * @return 结果
     */
    @Override
    public int deleteErpSurveyRefereeInfoById (String id) {
        return erpSurveyRefereeInfoMapper.deleteErpSurveyRefereeInfoById ( id );
    }


    /**
     * 修改勘测基础模块的附件，更新relationId为勘测记录Id
     * @param fileEntities
     * @param relationId
     */
    private void updateFile (List<FileEntity> fileEntities, String relationId) {
        if ( fileEntities != null && fileEntities.size () > 0 ) {
            fileEntities.forEach ( fileEntity -> {
                fileEntity.setRelationId ( relationId );
                fileEntity.setChangeTime ( DateUtils.getNowDate () );
            } );
            boolean b = iFileInfoService.updateBatchById ( fileEntities );
            if ( ! b ) {
                throw new RuntimeException ( "修改路条人附件失败" );
            }
        }
    }
}
